from google.appengine.ext import webapp
from google.appengine.ext import db
import sys, datetime, os, urllib2, re, model, themeController, utils, logging
import customThemeHandler as cth

class AccessViaShortUrl(webapp.RequestHandler):
	"""
	Legacy short URL ... just forwarding to the corresponding base64padless
	"""
	def get(self, nb):
		self.redirect("/" + utils.encodeB64Padless(nb))


class AccessViaB64ShortUrl(webapp.RequestHandler):
	"""
	New short URL access. URLs are like http://<domain>/<base64padless>
	Will call the CountDownDMYHMS handler which is the true main entry point
	"""
	def get(self, nb):
		nb = utils.decodeB64Padless(nb)
		if nb:
			# Get the full url corresponding to count
			q = db.GqlQuery("SELECT * FROM URLs WHERE counter = :1", nb)
			urls = q.fetch(1)
			if len(urls) == 1:
				matches = re.findall(utils.regexp["parseFullUrlWithSeconds"], urls[0].url)
				if len(matches) == 0:
					matches = re.findall(utils.regexp["parseFullUrl"], urls[0].url)
				try:				
					target = matches[0][0]
					options = matches[0][1] or ""
					title = matches[0][2] or ""
					subHandler = CountDownDMYHMS()
					subHandler.request = self.request
					subHandler.response = self.response
					subHandler.get(target, options, title)
				except:
					#print sys.exc_info()
					# TODO: need error management for the user here
					self.redirect("/")
			else:
				#print sys.exc_info()
				# TODO: need error management for the user here
				self.redirect("/")
		else:
			# TODO: need error management for the user here
			self.redirect("/")


class CountDownDMYHM(webapp.RequestHandler):
	"""
	Legacy full URL entry point, with no seconds
	Just adds 00 at the end of the URL and forwards to the CountDownDMYHMS handler
	"""
	def get(self, target, options, title):
		if options == None:
			options = ""
		if title == None:
			title = ""
		else:
			title = "/" + title
		self.redirect("/" + target + "00" + options + title)


class CountDownDMYHMS(webapp.RequestHandler):
	"""
	Real entry point to the site, takes in a full URL like http://<domain>/<ddmmyyyyhhmmss><options>/<title>
	Responsibility is to display the corresponding countdown and increment usage counter for URL
	"""
	def get(self, target, optionsStr, title):
		# Expected format is DDMMYYYYHHMMSS (checked via main.py by a regexp anyway)
		day = target[:2]
		month = target[2:4]
		year = target[4:8]
		hour = target[8:10]
		minute = target[10:12]
		second = target[12:14]
		
		if utils.isValidDate(day, month, year, hour, minute, second):
		
			storedUrl = utils.storeUrl(self.request.url, target, optionsStr or "", title or "", None, None)

			ctheme = None
			options = {}
		
			if storedUrl.customTheme:
				ctheme = storedUrl.customTheme.html
			else:
				options = themeController.parseOptions(optionsStr)
		
			jsdate = month + "/" + day + "/" + year + " " + hour + ":" + minute + ":" + second

			if title:
				title = title.strip()
				if title != '':
					title = urllib2.unquote(title.strip())
			else:
				title = ""
		
			urlToShare = utils.getContextRootFromUrl(self.request.url) + "/" + utils.encodeB64Padless(str(storedUrl.counter))
		
			data = {
				"dateObject" : {
					"day": day, "month": month, "year": year, "hour": hour, "minute": minute
				},
				"jsdate": jsdate,
				"title": title,
				"options": options,
				"customTheme": ctheme,
				"form": {},
				"up": storedUrl.up,
				"shortUrl": urlToShare,
				"listOfThemes": themeController.getListOfThemes()
			}
			data["latestUrls"] = utils.getLatestUrls()
			data["popularUrls"] = utils.getMostPopulars()
		
			formData = utils.prepareFormData()
			data["form"].update(formData)
		
			pageContent = utils.getTemplateContent(os.path.join(os.path.dirname(__file__), "templates/main.html"), data)
			self.response.out.write(pageContent)
			
		else:
			# Error, the date has a valid format, but is not a real date
			self.redirect("/")


class CreateCountdown(webapp.RequestHandler):
	"""
	Called when a new countdown URL is created
	In fact simply needs to redirect to the encoded b64padless version
	TODO: When managing custom themes, will also need to take them into account here
	"""
	def post(self):
		self.doCreate()
	def get(self):
		self.doCreate()
	def doCreate(self):
		target = self.request.get("datestamp")
		title = self.request.get("title")
		up = self.request.get("up")
		ctheme = None
		options = None
		
		# Handling custom themes or normal themes
		customTheme = self.request.get("customTheme")
		if customTheme:
			ctheme = cth.storeNewCustomTheme(customTheme)
		else:
			options = self.request.get("music") + self.request.get("theme")
		
		storedUrl = utils.storeUrl(self.request.url, target, options, title, ctheme, up)
		
		#self.response.out.write(utils.encodeB64Padless(str(storedUrl.counter)))
		self.redirect("/" + utils.encodeB64Padless(str(storedUrl.counter)))


class CreateCustomThemedCountdown(webapp.RequestHandler):
	"""
	Called when a user wants to start creating a countdown with an advanced custom theme
	"""
	def get(self):
		pageContent = utils.getTemplateContent(os.path.join(os.path.dirname(__file__), "templates/customThemePlayGround.html"), {"expertMode": True})
		self.response.out.write(pageContent)


class CreateSimpleCustomThemedCountdown(webapp.RequestHandler):
	"""
	Called when a user wants to start creating a countdown with simple custom theme
	"""
	def get(self):
		pageContent = utils.getTemplateContent(os.path.join(os.path.dirname(__file__), "templates/customThemePlayGround.html"), {"simpleCustomMode": True})
		self.response.out.write(pageContent)


class GetSafeHtml(webapp.RequestHandler):
	"""
	Called via XHR to tame the html during custom theme preview action
	"""
	def post(self):
		html = self.request.get("html")
		safehtml = cth.getSafeCustomTheme(html)
		self.response.out.write(safehtml)


class Index(webapp.RequestHandler):
	"""
	Fallback handler in case no URL pattern matched.
	Shows a fake timer to urge users to create their own
	"""
	def get(self):
		delta = datetime.datetime.today() + datetime.timedelta(hours = 1)

		day = str(delta.day)
		if len(day) == 1: day = "0" + day
		month = str(delta.month)
		if len(month) == 1: month = "0" + month
		year = str(delta.year)
		hour = str(delta.hour)
		if len(hour) == 1: hour = "0" + hour
		minute = str(delta.minute)
		if len(minute) == 1: minute = "0" + minute
		second = str(delta.second)
		if len(second) == 1: second = "0" + second
		
		data = {
			"dateObject" : {
				"day": day, "month": month, "year": year, "hour": hour, "minute": minute
			},
			"jsdate": month + "/" + day + "/" + year + " " + hour + ":" + minute + ":" + second,
			"title": "w.aitfor.it",
			"options": themeController.parseOptions("waitforit"),
			"form": {},
			"up": True,
			"shortUrl": self.request.url,
			"listOfThemes": themeController.getListOfThemes(),
			"nbOfTotalUrls": utils.getNbOfTotalUrls()
		}
		data["latestUrls"] = utils.getLatestUrls()
		data["popularUrls"] = utils.getMostPopulars()
		
		formData = utils.prepareFormData()
		data["form"].update(formData)
		
		pageContent = utils.getTemplateContent(os.path.join(os.path.dirname(__file__), "templates/main.html"), data)
		self.response.out.write(pageContent)